//
//  NetRequestClss.m
//  DataStatistics
//
//  Created by Kang on 15/12/9.
//  Copyright © 2015年 YTYangK. All rights reserved.
//
//  请求格式
//
/**
 要使用常规的AFN网络访问
 
 1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 
 所有的网络请求,均有manager发起
 
 2. 需要注意的是,默认提交请求的数据是二进制的,返回格式是JSON
 
 1> 如果提交数据是JSON的,需要将请求格式设置为AFJSONRequestSerializer
 2> 如果返回格式不是JSON的,
 
 3. 请求格式
 
 AFHTTPRequestSerializer            二进制格式
 AFJSONRequestSerializer            JSON
 AFPropertyListRequestSerializer    PList(是一种特殊的XML,解析起来相对容易)
 
 4. 返回格式
 
 AFHTTPResponseSerializer           二进制格式
 AFJSONResponseSerializer           JSON
 AFXMLParserResponseSerializer      XML,只能返回XMLParser,还需要自己通过代理方法解析
 AFXMLDocumentResponseSerializer (Mac OS X)
 AFPropertyListResponseSerializer   PList
 AFImageResponseSerializer          Image
 AFCompoundResponseSerializer       组合
 */

#import "NetRequestClss.h"
#import "AFNetworking.h"
//#import "JSONKit.h"
#import "AFHTTPSessionManager.h"
//#import "AFNetworkActivityIndicatorManager.h"
#import "UtilToolsClss.h"
#import "TabBarC.h"
#import "MoreViewController.h"
#import "JPUSH.h"

@interface NetRequestClss ()
@end

@implementation NetRequestClss

static NetRequestClss  *instance = nil;
static dispatch_once_t onceToken;

/** 网络状态
 AFNetworkReachabilityStatusUnknown          = -1,  // 未知
 AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
 AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G 花钱
 AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络,不花钱
 */
//static int netWorkState;

+ (NetRequestClss *)sharedNetRequestClss {
    dispatch_once(&onceToken, ^{
        if (instance == nil) {
            instance = [[super allocWithZone:NULL] init];
        }
    });
    return instance;
}
// 统一的一个请求管理者对象
- (AFHTTPSessionManager *)sessionManager {
    if (_sessionManager == nil) {
        return _sessionManager = [AFHTTPSessionManager manager];
    }
    return _sessionManager;
}


- (AFHTTPResponseSerializer *)rSerializer {
    if (_rSerializer == nil) {
       return _rSerializer = [AFHTTPResponseSerializer serializer];
    }
    return _rSerializer;
}

#pragma mark - 检测网络连接
// 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
+ (void)netReachabilityForReturnBlock:(ReturnValueBlock)block {
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       // netWorkState = status;
        NSLog(@"----->%ld",(long)status);
        block(@(status));
    }];
}

/// 外调 监控网络
/*+ (void)netWorkReachabilityWithRequestBlock: (ReturnValueBlock)block {
    __block id number = @201600;
    // 创建网络监听者管理者对象
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
// 设置监听
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                number = @201601;
                NSLog(@"未识别的网络");
                [MBProgressHUD showError:NSLocalizedString(@"NetRClss1", nil)];
                break;
            case AFNetworkReachabilityStatusNotReachable:
                number = @201602;
                NSLog(@"不可达的网络(未连接)");
                [MBProgressHUD showError:NSLocalizedString(@"NetRClss1", nil)];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                number = @201603;
                NSLog(@"2G,3G,4G...的网络");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                number = @201604;
                 NSLog(@"wifi的网络");
                break;
            default:
                number = @201605;
                NSLog(@"特殊情况");
                [MBProgressHUD showError:NSLocalizedString(@"NetRClss2", nil)];
                break;
        }
          block(number);
    }];
    // 开始监听
    [manager startMonitoring];
}
*/


/// 外调-监控url
+ (BOOL)netWorkReachabilityWithURLString:(NSString *)strUrl {
    __block BOOL netState = NO;
    NSString *urlStr = [[NSString alloc] initWithFormat:@"%@%@",IPHEAD,strUrl];
    NSURL *baseURL = [NSURL URLWithString:urlStr];  // 接收链接
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:baseURL];
    
    NSOperationQueue *operationQueue = manager.operationQueue; //纳秒运算队列
    
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [operationQueue setSuspended:NO];
                netState = YES;
                break;
            //case AFNetworkReachabilityStatusUnknown:       //未知的网络
            case AFNetworkReachabilityStatusNotReachable:  //没有网络
                netState = NO;
               
                break;
            default:
                [operationQueue setSuspended:YES]; //暂停的
                break;
        }
       
    }];

    [manager.reachabilityManager startMonitoring];
     return netState;

}



/// 内调-监控网络
+ (BOOL)beforeExecute:(AFHTTPSessionManager *)manager {
    NSOperationQueue *operationQueue = manager.operationQueue;
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [operationQueue setSuspended:NO];  //开启请求
                break;
            case AFNetworkReachabilityStatusNotReachable:
             default:
                [operationQueue setSuspended:YES]; //暂停请求

                break;
        }
    }];
    [manager.reachabilityManager startMonitoring];
    return true;
}


/// 监控提示
+ (NSString *)afterExecute:(NSError *)error addViewC:(UIViewController *)vc {
//    if (error == nil) {
//        return @"---";//true;
//    }
    
    if ([error code] == NSURLErrorNotConnectedToInternet) {
        //@"网络不给力"
        return NSLocalizedString(@"NetRClss3701", nil);//false;
    }else if([error code] == NSURLErrorTimedOut) {
        
        // @"请求超时,请你稍后尝试。"
        return NSLocalizedString(@"NetRClss3705", nil);
    }else if([error code] == NSURLErrorBadServerResponse) {
        // @"服务器异常,该网页不存在"
        return NSLocalizedString(@"NetRClss3704", nil);
    }//else if([error code] == NSURLErrorCannotConnectToHost) {
        return NSLocalizedString(@"NetRClss3703", @"服务器维护，请稍后再试");
   // }
    
    // 服务器故障，请稍后再试
    //return true;
}

#pragma mark - 使用这个请求方法，其他不使用。
+ (void)requestWithUrl:(NSString *)url requestWithParameters:(NSDictionary *)par method:(NetMethod)met returnSuccess:(void(^)(id objs, int status, NSString *mag))success returnError:(void(^)(NSString *err))err {

    NSString *metStr = (met > 0) ? @"POST" : @"GET";

    NSString *requestUrl = [NSString stringWithFormat:@"%@%@",IPHEAD,url];
    NSLog(@"接口打印 ============  %@",requestUrl);

    [[AFNetworkReachabilityManager sharedManager]startMonitoring];
    
    [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (status == AFNetworkReachabilityStatusNotReachable) {
            
            NSError *errer =nil ;
            
            [NetRequestClss requestWithReturnError:errer errorBlock:err];
            
        }else{
    
    // AF 管理者
    AFHTTPSessionManager *manager = [NetRequestClss sharedNetRequestClss].sessionManager;
    
    if (![NetRequestClss beforeExecute:manager]) {
        return;
    }
    
    manager.responseSerializer = [NetRequestClss sharedNetRequestClss].rSerializer;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 10.f;//3
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    
    
     NSComparisonResult getComparison = [metStr caseInsensitiveCompare:@"GET"];
    if (getComparison == NSOrderedSame) {
        //requestUrl.URL.absoluteString
        
        
        [manager GET:[NSString stringWithFormat:@"%@",requestUrl] parameters:par progress:^(NSProgress * _Nonnull downloadProgress) {
            //不做操作
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"----->%@",responseObject);
            [NetRequestClss requestWithRetrunSuccess:responseObject successBlock:success];

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [NetRequestClss requestWithReturnError:error errorBlock:err];

        }];
        
    }
    
    
    NSComparisonResult postComparison = [metStr caseInsensitiveCompare:@"POST"];
    if (postComparison == NSOrderedSame) {

        [manager POST:[NSString stringWithFormat:@"%@",requestUrl] parameters:par progress:^(NSProgress * _Nonnull uploadProgress) {
            //不做操作
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [NetRequestClss requestWithRetrunSuccess:responseObject successBlock:success];

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [NetRequestClss requestWithReturnError:error errorBlock:err];

        }];
    }
    
        }
    }];
}


/**
 *  请求成功
 */
+ (void)requestWithRetrunSuccess:(id _Nonnull)responseObject successBlock:(void(^)(id,int,NSString *))block{
    // 请求返回的信息
    __block NSString *requestStr = @"";
    NSError *jsonError = nil;
    NSData *jsonData = nil;
    // 转成Json字符串
    NSString  *jsonStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
   // BOOL bol = [NSJSONSerialization isValidJSONObject:jsonStr];
//    if ([jsonStr rangeOfString:@"<"].location != NSNotFound) {
//        NSRange range = [jsonStr rangeOfString:@"<"];
//        jsonStr = [jsonStr substringToIndex:range.location];
//        jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
//    }else {
        jsonData = responseObject;
 //   }
    
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseObject
                          options:kNilOptions
                          error:&jsonError];
    
    
    //逼出
    if ([json[@"msg"] isEqualToString:@"无效令牌"]) {

        [[JPUSH sharedJPUSH] abnormalExit:@"无效令牌"];
        [MBProgressHUD yty_showErrorWithTitle:nil detailsText:@"当前账号在别的设备上登录!" toView:nil];
    }
    //         NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&jsonError];
    
         NSLog(@"获取json字符串：原型---->%@  Dic---%@",jsonStr,json);
    
        if (jsonStr != nil) {
            if (jsonError) {
                requestStr = @"转换字典失败，请查看数据";
                block(nil,NetObtainDataStatusFail,requestStr); // -1;
            }else {
                requestStr = @"请求成功!";
                block(json,NetObtainDataStatusSuccess,requestStr); // 1
            }
        }else {
            requestStr = @"获取不到任何数据";
            block(nil,NetObtainDataStatusSuccessAlsoNotData,requestStr); //2
        }
}
/**
 *  请求失败
 */
+ (void)requestWithReturnError:(NSError *_Nonnull)error errorBlock:(void(^)(NSString *err))block {
    NSString *requestStr = @"";
    [UIApplication sharedApplication].networkActivityIndicatorVisible  = NO;
     NSLog(@"\n获取到错误，API错误，或服务器连接失败-->error is %@",error);
    requestStr = [NetRequestClss afterExecute:error addViewC:nil];
     //@"api异常！请检查您的服务器！";
      block(requestStr);
}



#pragma mark - Url拼接 - 添加请求头部内容 -单件
+ (NSMutableURLRequest *)ANetRequestWithURLString:(NSString *)requestUrlString WithHeader:(NSDictionary *)header WithParameter:(NSDictionary *)parameter WithHTTPMethod:(NSString *)httpMethod {
    NSLog(@"---->%@",IPHEAD);
    NSString *strUrl = [NSString stringWithFormat:@"%@%@",IPHEAD,requestUrlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:25]; //请求时间10s
    
    //添加请求头
    if (header != nil) {
        for (NSString *key in header.allKeys) {
            [request addValue:header[key] forHTTPHeaderField:key];
            // [request setValue:header[key] forHTTPHeaderField:key];
        }
    }
    
    // get 请求
    NSComparisonResult GETComparison = [httpMethod caseInsensitiveCompare:@"GET"];
    if (GETComparison == NSOrderedSame) {
        [request setHTTPMethod:@"GET"];
        
        if (parameter != nil) {
            //添加参数，将参数拼接在url后面
            NSMutableString *paramsStr = [NSMutableString string];
            NSArray *allKeyAry = [parameter allKeys];
            for(NSString *key in allKeyAry) {
                NSString *vale = [parameter objectForKey:key];
                [paramsStr appendFormat:@"&%@=%@",key,vale];
            }
            
            
            if (paramsStr.length > 0) {
                [paramsStr replaceCharactersInRange:NSMakeRange(0, 1) withString:@"?"];
                //重新设置url
                NSLog(@"\n请求路径:GET-->requestUrl is %@",request);
                NSString *newStrUrl = [strUrl stringByAppendingString:paramsStr];
                [request setURL:[NSURL URLWithString:newStrUrl]];
                
                NSLog(@"\n请求路径:GET-->requestUrl is %@",request);
                
            }
        }
    }
    
    // post 请求
    NSComparisonResult POSTComparison = [httpMethod caseInsensitiveCompare:@"POST"];
    if (POSTComparison == NSOrderedSame) {
        [request setHTTPMethod:@"POST"];
        
        // 暂时不处理
//        for (NSString *key in parameter) {
//            [request setHTTPBody:parameter[key]];
//        }
        NSLog(@"\n请求路径:POST-->requestUrl is %@",request);
        
    }
    return request;
}




- (void)dealloc {
    NSLog(@"----->%@",self);
}

@end
